﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using GC2.Bussines;
using GC2.Bussines.BMAdmin;
using GC2.Entities.Admin;
using GC2.ExceptionHandler;

public partial class Proveedores_ProveedoresAdmin : GenericPage
{
    Proveedores proveedorActual = new Proveedores();    
    List<Proveedores> listProveedores = new List<Proveedores>();
    private ExceptionConstructor exNew = new ExceptionConstructor();

    protected void Page_Load(object sender, EventArgs e)
    {
        SetearCultura();
        SetearConexion();
    }

    #region Controles

    protected void btnBuscar_Click(object sender, EventArgs e)
    {
        try
        {
            BMProveedores bmProveedores = new BMProveedores(Connection);
            if (txtCodigo.Text.Trim() != "")
            {
                proveedorActual.Id_Proveedor = Convert.ToInt16(txtCodigo.Text.Trim());
            }
            else
            {
                proveedorActual.Id_Proveedor = 0;
            }

            proveedorActual.Nombre = txtNombre.Text.Trim();

            listProveedores = bmProveedores.BMProveedoresSearch(proveedorActual);

            Session["listProveedores"] = listProveedores;
            CargarGrilla();

        }
        catch (Exception ex)
        { throw ex; }
    }

    protected void btnNuevo_Click(object sender, EventArgs e)
    {
        Session["Accion"] = 1;
        Response.Redirect("ProveedoresGestion.aspx", false);
    }

    protected void btnCancelar_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Seguridad/Default.aspx", false);
    }

    protected void gvProveedores_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvProveedores.PageIndex = e.NewPageIndex;
        CargarGrilla();
    }

    protected void gvProveedores_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }

    protected void gvProveedores_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            BMProveedores bmProveedores = new BMProveedores(Connection);
            Proveedores proveedor = new Proveedores();
            proveedor.Id_Proveedor = Convert.ToInt64(gvProveedores.SelectedDataKey.Value);
            proveedorActual = proveedor;
            proveedor = bmProveedores.BMProveedoresGetOne(proveedorActual);

            Session["Proveedor"] = proveedor;
            Session["Accion"] = 2;
            Response.Redirect("ProveedoresGestion.aspx", false);
        }
        catch (Exception ex)
        {
            ErrorUI(ex);
        }
    }

    #endregion

    #region Métodos
    private void CargarGrilla()
    {
        try
        {
            listProveedores = (List<Proveedores>)Session["listProveedores"];
            gvProveedores.DataSource = listProveedores;
            gvProveedores.DataBind();
        }
        catch (Exception ex)
        { throw ex; }
    }

    #endregion

}
